package com.hulk.rpc.config.common;

import cn.hutool.core.lang.Assert;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuxukang
 * @date 2019-03-09 09:39
 */
@Data
public final class HostPort {

  private final String host;
  private final Integer port;

  public HostPort(String host, Integer port) {
    Assert.notBlank(host);
    Assert.notNull(port);
    this.host = host;
    this.port = port;
  }

  public HostPort(String hostAndPort) {
    String[] split = StringUtils.split(hostAndPort, ":");
    Assert.isTrue(split.length == 2, "the param must like xxx:xxx");
    this.host = split[0];
    this.port = Integer.valueOf(split[1]);
  }

  public static List<HostPort> parse(String str) {
    String[] array = str.split(",");
    List<HostPort> hostPortList = new ArrayList<HostPort>(array.length);

    for (String s : array) {
      hostPortList.add(new HostPort(s));
    }

    return hostPortList;
  }
}
